package com.ruoyi.cms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cms.domain.CmsModelField;
import com.ruoyi.cms.mapper.CmsModelMapper;
import com.ruoyi.cms.domain.CmsModel;
import com.ruoyi.cms.service.ICmsModelService;
import com.ruoyi.common.core.text.Convert;

/**
 * 数据模型Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-29
 */
@Service
public class CmsModelServiceImpl implements ICmsModelService 
{
    @Autowired
    private CmsModelMapper cmsModelMapper;

    /**
     * 查询数据模型
     * 
     * @param modelId 数据模型ID
     * @return 数据模型
     */
    @Override
    public CmsModel selectCmsModelById(Long modelId)
    {
        return cmsModelMapper.selectCmsModelById(modelId);
    }

    /**
     * 查询数据模型列表
     * 
     * @param cmsModel 数据模型
     * @return 数据模型
     */
    @Override
    public List<CmsModel> selectCmsModelList(CmsModel cmsModel)
    {
        return cmsModelMapper.selectCmsModelList(cmsModel);
    }

    /**
     * 新增数据模型
     * 
     * @param cmsModel 数据模型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCmsModel(CmsModel cmsModel)
    {
        cmsModel.setStatus("0");
        cmsModel.setDelFlag("0");
        cmsModel.setCreateTime(DateUtils.getNowDate());
        int rows = cmsModelMapper.insertCmsModel(cmsModel);
        insertCmsModelField(cmsModel);
        return rows;
    }

    /**
     * 修改数据模型
     * 
     * @param cmsModel 数据模型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCmsModel(CmsModel cmsModel)
    {
        cmsModel.setDelFlag(null);
        cmsModel.setUpdateTime(DateUtils.getNowDate());
        cmsModelMapper.deleteCmsModelFieldByModelId(cmsModel.getModelId());
        insertCmsModelField(cmsModel);
        return cmsModelMapper.updateCmsModel(cmsModel);
    }

    /**
     * 删除数据模型对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsModelByIds(String ids)
    {
        cmsModelMapper.deleteCmsModelFieldByModelIds(Convert.toStrArray(ids));
        return cmsModelMapper.deleteCmsModelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除数据模型信息
     * 
     * @param modelId 数据模型ID
     * @return 结果
     */
    @Override
    public int deleteCmsModelById(Long modelId)
    {
        cmsModelMapper.deleteCmsModelFieldByModelId(modelId);
        return cmsModelMapper.deleteCmsModelById(modelId);
    }

    /**
     * 新增数据模型字段信息
     * 
     * @param cmsModel 数据模型对象
     */
    public void insertCmsModelField(CmsModel cmsModel)
    {
        List<CmsModelField> cmsModelFieldList = cmsModel.getCmsModelFieldList();
        Long modelId = cmsModel.getModelId();
        if (StringUtils.isNotNull(cmsModelFieldList))
        {
            List<CmsModelField> list = new ArrayList<CmsModelField>();
            for (CmsModelField cmsModelField : cmsModelFieldList)
            {
                cmsModelField.setModelId(modelId);
                list.add(cmsModelField);
            }
            if (list.size() > 0)
            {
                cmsModelMapper.batchCmsModelField(list);
            }
        }
    }
}
